<template>
  <el-dialog title="申请详情" :visible.sync="visible" :close-on-click-modal="false" append-to-body>
    <el-form ref="main-form" status-icon :model="mainQuery" :rules="rules" label-width="120px" label-position="left" :inline="false" style="width: 70%">

      <el-form-item     label="销售员" size="mini" prop="">
        <span v-text="data.realName"></span>
      </el-form-item>

      <el-form-item     label="统计年月" size="mini" prop="">
        <span v-text="data.statMonth"></span>
      </el-form-item>

      <el-form-item     label="转账金额" size="mini" prop="">
        <span v-text="`${data.transAmount} ${data.transCurrency}`"></span>
      </el-form-item>

      <el-form-item     label="转帐状态" size="mini" prop="">
        <span v-text="startList[data.handleStatus]"></span>
      </el-form-item>

      <el-form-item     label="转帐方式" size="mini" prop="">
        <span v-text="data.strTransType"></span>
      </el-form-item>

      <template v-if="data.transType == 1">
        <el-form-item     label="开户行名称" size="mini" prop="">
          <span v-text="data.bankAccountName"></span>
        </el-form-item>
        <el-form-item     label="开户账号" size="mini" prop="">
          <span v-text="data.bankAccountNo"></span>
        </el-form-item>
        <el-form-item     label="开户行行号" size="mini" prop="">
          <span v-text="data.bankOpenBankNo"></span>
        </el-form-item>

        <el-form-item     label="是否通过API" size="mini" prop="">
          <el-radio-group v-model="mainQuery.useApi"  :disabled="data.handleStatus ==1">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <template v-if="mainQuery.useApi">
          <el-form-item     label="通道" size="mini" prop="">
            <el-select v-model="channelId" placeholder="请选择" clearable  :disabled="data.handleStatus ==1">
              <el-option v-for="item in chaninOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </template>

      </template>

      <template v-if="data.transType == 2">
        <el-form-item     label="平台商户邮箱" size="mini" prop="">
          <span v-text="data.mchEmail"></span>
        </el-form-item>
      </template>


      <el-form-item label="审核意见" size="mini" prop="remark">
        <el-input placeholder="" type="textarea" v-model="mainQuery.remark" clearable></el-input>
      </el-form-item>

      <template>
        <div>说明</div>
        <ol>
          <li>若通过API提现/转帐,只需点击审核通过,系统自动处理</li>
          <li>若不为API提现/转帐,则需要人工转账,确认对方到账后,才点击审核通过</li>
        </ol>
      </template>

    </el-form>

    <div slot="footer" class="dialog-footer">

      <el-button @click="close" size="mini">取 消</el-button>

      <el-popover v-if="rejectVisible" placement="top" width="160" v-model="popRejectVisible">
        <p>确定驳回审核吗?</p>
        <div style="text-align: right; margin: 0">
          <el-button size="mini" type="default" @click="popRejectVisible = false">取消</el-button>
          <el-button type="danger" size="mini" @click="submit(false)" :loading="loading">驳回</el-button>
        </div>
        <el-button slot="reference" size="mini" type="danger" :loading="loading" style="float: left">驳回审核</el-button>
      </el-popover>

      <el-popover  placement="top" width="160" v-model="popConfirmVisible">
        <p>确定通过审核吗?</p>
        <div style="text-align: right; margin: 0">
          <el-button size="mini" type="default" @click="popConfirmVisible = false">取消</el-button>
          <el-button type="primary" size="mini" @click="submit(true)" :loading="loading">通过</el-button>
        </div>
        <el-button slot="reference" size="mini" type="primary" :loading="loading" style="margin-left: 10px">通过审核</el-button>
      </el-popover>
    </div>
  </el-dialog>
</template>

<script>

import ApiMain from '@/api/commission.salesTrans';

export default {
  mixins: [
  ],
  props: [
  ],
  data() {
    return {
      channelId:"",
      mainQuery: {
        useApi   : false,
        channelId: '',
        isApprove: "",
        remark   : "",
      },
      visible: false,
      rejectVisible: false,
      currentRemarkVisible: false,
      popConfirmVisible: false,
      popRejectVisible: false,
      chaninOptions: [],
      // 0.初审待审 1.初审通过 2.初审驳回 3.复审通过 4.复审驳回
      startList: {
        0: '初审待审',
        1: "初审通过",
        2: "初审驳回",
        4: "复审通过",
        4: "复审驳回"
      },
      loading: false,
      rules: {
        remark: [
          { required: true, message: '请填写内容', trigger: 'blur' },
        ],
      },

      id: null,
      data: {},

      action: '',
      strType: '',
      currentRemark: '',

    }
  },
  mounted() {
    this.mainQueryRaw = Object.copy(this.mainQuery);
  },

  methods: {

    async show(data, action) {

      console.log(action);

      this.reset();

      this.data = data;

      this.action               = action;
      this.rejectVisible        = true;
      this.currentRemarkVisible = true;

      switch (action) {
        case 'recheck':
          this.mainQuery.useApi    = !!data.transByApi;
          this.mainQuery.channelId = data.transChannelId || '';
          break;
      }

      // console.log(JSON.stringify(data, null, 1));

      this.visible = true;

      this.chaninOptions = await ApiMain.getOptions({ canTransfer: 1 })

    },

    close() {
      this.visible = false;
      this.$emit('dialog-close');
    },

    reset() {
      this.$refs['main-form'] && this.$refs['main-form'].resetFields();
      this.id = null;
      this.data = null;
    },

    verifyForm() {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit(isConfirm) {

      this.popConfirmVisible = false;
      this.popRejectVisible = false;

      var req       = Object.copy(this.mainQuery);
      req.id        = this.data.id;
      req.isApprove = isConfirm;

      // console.log(JSON.stringify(req, null, 1));

      this.loading = true;

      // console.log(this.channelId)

      var re = null;

      try {

        if (this.action == 'check') {
          re = await ApiMain.withdrawFirstReview(req);
        }
        else {
          re = await ApiMain.withdrawSecondReview(req);
        }

        this.$message({ message: '操作成功', type: 'success' });
        this.close();

      } catch (e) { console.error(e) }

      this.loading = false;
    },

  }
}
</script>

<style>
</style>
